--
-- Author: ping.wang
-- Date: 2018-08-14 17:54:06
--
-- RewardMonthSignRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardMonthSignRender,true,true))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._aniImg = self._ui.ani_img --可以领取的时候显示此动画层 并循环播放
    self._dayNum = self._ui["Panel/num"] --第几天的
    self._iconOver = self._ui.icon_over --已经领取的标识
    self._grade = self._ui["Panel/icon/grade"] --道具品质框
    self._icon = self._ui["Panel/icon/icon"] --道具图片
    self._itemNum = self._ui["Panel/icon/Panel_num/num"] --道具数量

    self._ui["Panel/icon"]:setTouchEnabled(false)

    self._ui.Panel:setTouchEnabled(true)
    self._ui.Panel:addTouchEventListener(function( sender, eventType )
        if eventType == ccui.TouchEventType.began then
            self._delayAct = sender:performWithDelay(function()
                self._delayAct = nil

                local worldPos = self._ui.Panel:convertToWorldSpace(cc.p(0,0))
                self._tipPos = self._tipPos or {x=2/5, y=1/2}
                if worldPos.x < display.cx then
                    self._tipPos.x = 5/8
                else
                    self._tipPos.x = 1/4
                end
                self:showTip()
            end, 0.1)
        elseif eventType == ccui.TouchEventType.ended then
            if self._delayAct then
                sender:stopAction(self._delayAct)
                self._delayAct = nil
            else
                UIHandler:hideFloatPanel()
            end

            if self._couldGet then
                executeFunc(self._onClick, "getMonthSignAward", {uuid = self._activityId, idx = self._data.idx})
            end
        end
    end)

    self:getTimeline():gotoFrameAndPause( 0 )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        local data = self._data
        self._iconOver:setVisible(data.haveGet)
        local itemData = self._data.itemData
        self._dayNum:setString(tostring(data.idx))
        self._itemNum:setString(tostring(itemData:getAmount()))
        self._icon:loadTexture(itemData:getIcon())
        self._grade:loadTexture(itemData:getIconBg())
    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

--设置是否可领取
function ClassRef:setCanGet(bl)
    if bl and not self._data.haveGet then
        self._aniImg:setVisible(true)
        self:getTimeline():gotoFrameAndPlay(0, true)
    else
        self._aniImg:setVisible(false)
        self:getTimeline():gotoFrameAndPause( 0 )
    end
    self._couldGet = bl
    self._ui.Panel:setTouchEnabled(not self._data.haveGet)
end

function ClassRef:showTip()
    local itemData = self._data.itemData
    if itemData then
        local type = itemData.itemBagType
        local data = itemData

        local param = { itemId=tonumber(data.tid) }
        local tipsType
        if type == Enums.ItemBagType.daibi then
            tipsType = Enums.TipsType.daibi
        elseif type == Enums.ItemBagType.mount then
            tipsType = Enums.TipsType.mount
        elseif type == Enums.ItemBagType.equip then
            tipsType = Enums.TipsType.equip
        elseif type == Enums.ItemBagType.jewel then
            tipsType = Enums.TipsType.jewel
        elseif type == Enums.ItemBagType.fame then -- 声望的tips当代币处理
            param.itemId = tonumber(data.uid)
            tipsType = Enums.TipsType.fame
        elseif type == Enums.ItemBagType.mountSoul then
            param.itemId = tonumber(data.id)
            tipsType = Enums.TipsType.mountSoul
        elseif type == Enums.ItemBagType.partnerSoul then
            param.itemId = tonumber(data.id)
            tipsType = Enums.TipsType.partnerSoul
        else
            tipsType = Enums.TipsType.item
        end

        param.dataTemp = data
        param.tipsType = tipsType
        param.unShowOrigin = self._unShowOrigin
        param.closeCallBack = nil
        local boundingBoxTemp = self:getBoundingBox()
        local worldPosTemp = self:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
        boundingBoxTemp.x = worldPosTemp.x
        boundingBoxTemp.y = worldPosTemp.y
        param.boundingBox = boundingBoxTemp
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
    end
end

function ClassRef:setActivityId(uuid)
    self._activityId = uuid
end

return ClassRef
